package dice.sinanya.dice.manager;


import com.forte.qqrobot.beans.messages.types.MsgGetTypes;
import dice.sinanya.db.npclist.InsertNpcList;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.CantInPrivateException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nlpcn.commons.lang.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static dice.sinanya.system.MessagesKp.KP_GROUP;
import static dice.sinanya.system.MessagesTag.TAG_NPC_MOVE;
import static dice.sinanya.system.MessagesTag.TAG_NPC_ST;
import static dice.sinanya.system.NpcListCache.NPC_LIST;
import static dice.sinanya.tools.checkdata.CheckList.checkNpc;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entitySystemProperties;
import static dice.sinanya.tools.makedata.MakeMessages.deleteTag;
import static dice.sinanya.tools.makedata.Sender.sender;
import static dice.sinanya.tools.makedata.StringFormatById.stringFormatById;

public class NpcList {
    private static final Pattern funSep = Pattern.compile("-([^0-9])");
    private static final Logger log = LogManager.getLogger(NpcList.class.getName());
    private final EntityTypeMessages entityTypeMessages;
    private final Pattern plus = Pattern.compile("[+*/\\-]");
    InsertNpcList insertNpcList = new InsertNpcList();

    public NpcList(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    public void st() throws CantInPrivateException {
        String tag = TAG_NPC_ST;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        Matcher sepMatcher = funSep.matcher(msg);
        if (sepMatcher.find()) {
            String role = msg.split(funSep.toString())[0].trim().toLowerCase();
            String properties = sepMatcher.group(1) + msg.split(funSep.toString())[1];
            String groupId = entityTypeMessages.getFromGroupString();
            if (entityTypeMessages.getMsgGetTypes().equals(MsgGetTypes.privateMsg)) {
                if (KP_GROUP.containsKey(entityTypeMessages.getFromQqString())) {
                    groupId = KP_GROUP.get(entityTypeMessages.getFromQqString());
                    insertNpcList.insertNpcList(groupId, role, properties);
                    sender(entityTypeMessages, stringFormatById(entitySystemProperties.getNpcInsertForKp(entityTypeMessages.getFromGroupString()), role, groupId));
                } else {
                    sender(entityTypeMessages, entitySystemProperties.getNpcNeedKp(entityTypeMessages.getFromGroupString()));
                }
            } else {
                insertNpcList.insertNpcList(groupId, role, properties);
                sender(entityTypeMessages, stringFormatById(entitySystemProperties.getNpcInsert(entityTypeMessages.getFromGroupString()), role));
            }
        } else {
            sender(entityTypeMessages, entitySystemProperties.getNpcFormatError(entityTypeMessages.getFromGroupString()));
        }
    }

    public void remove() throws CantInPrivateException {
        checkCantPrivate();
        String tag = TAG_NPC_MOVE;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        String groupId = entityTypeMessages.getFromGroupString();
        insertNpcList.deleteNpcList(groupId, msg);
        sender(entityTypeMessages, stringFormatById(entitySystemProperties.getNpcDelete(entityTypeMessages.getFromGroupString()), msg));
    }

    public void clr() throws CantInPrivateException {
        checkCantPrivate();
        String groupId = entityTypeMessages.getFromGroupString();
        insertNpcList.deleteNpcListByGroup(groupId);
        sender(entityTypeMessages, entitySystemProperties.getNpcClear(entityTypeMessages.getFromGroupString()));
    }

    public void show() throws CantInPrivateException {
        checkCantPrivate();
        String groupId = entityTypeMessages.getFromGroupString();
        ArrayList<String> result = new ArrayList<>();
        result.add(entitySystemProperties.getNpcShow(entityTypeMessages.getFromGroupString()));
        if (checkNpc(groupId)) {
            HashMap<String, HashMap<String, Integer>> NPC_TMP = NPC_LIST.get(groupId);
            for (Map.Entry<String, HashMap<String, Integer>> propForName : NPC_TMP.entrySet()) {
                ArrayList<String> tmp = new ArrayList<>();
                String name = propForName.getKey();
                int hp = propForName.getValue().getOrDefault("hp", 0);
                int san = propForName.getValue().getOrDefault("san", 0);
                int mp = propForName.getValue().getOrDefault("mp", 0);
                tmp.add(name);
                tmp.add("hp:" + hp);
                if (san != 0) {
                    tmp.add("san:" + san);
                }
                if (mp != 0) {
                    tmp.add("mp:" + mp);
                }
                result.add(StringUtil.joiner(tmp, "\t"));
            }
        }
        sender(entityTypeMessages, StringUtil.joiner(result, "\n"));
    }

    private void checkCantPrivate() throws CantInPrivateException {
        if (entityTypeMessages.getMsgGetTypes().equals(MsgGetTypes.privateMsg)) {
            throw new CantInPrivateException(entityTypeMessages);
        }
    }
}
